package de.tabuma.sso.sso;

import java.util.*;
import java.util.logging.*;
import de.tabuma.sso.groupprivileg.*;
import de.tabuma.sso.logik.*;
import de.tabuma.sso.privileg.*;
import de.tabuma.sso.user.*;
import de.tabuma.sso.usergroup.*;
import de.tabuma.sso.group.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.2.2.1.2.1 $
 * @since $Date: 2010/01/13 10:23:10 $
 */
public class SSOFacade {

    private static Logger log = Logger.getLogger(SSOFacade.class.getName());

    public static String getSSO(String username, String password) {
        String keys[] = new String[2];
        keys[0] = "name";
        keys[1] = "password";
        String values[] = new String[2];
        values[0] = username;
        values[1] = password;
        List<User> uList = PMF.get(keys, values, new User());
        log.warning("uList.size():"+uList.size());
        if(uList.size()>0) {
            log.warning(""+uList);
            log.warning(""+uList.get(0));
            User user = uList.get(0);
            String ssoID = createSSOID();
            SSO sso = new SSO();
            sso.setDate(Calendar.getInstance().getTime());
            sso.setSsoId(ssoID);
            sso.setUserId(user.getId().intValue());
            PMF.doCreate(sso, username+"("+user.getId()+")");
            return ssoID;
        }
        return "";
    }

    private static String createSSOID() {
        char character[] = {'a','b','c','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'};
        int length = (int)(Math.random()*30)+20;
        String ssoId = "";
        for(int i=0;i<length;i++) {
            int index = (int) (Math.random()*character.length);
            ssoId += character[index];
        }
        return ssoId;
    }
    
    public static SSO get(String ssoId) {
        String key[] = new String[1];
        key[0] = "ssoId";
        String value[] = new String[1];
        value[0] = ssoId;
        List<SSO> sList = PMF.get(key, value, new SSO());
        if(sList.size()>0) {
            return sList.get(0);
        }
        return null;
    }

    public static boolean sessionIsActiv(String ssoId) {

        SSO sso = get(ssoId);
        if(sso==null) {
            return false;
        }
        if(sso.getLastTimeInMinutes()>30) {
            return false;
        }
        sso.setDate(Calendar.getInstance().getTime());
        return true;
    }

    public static int userIsAdmin(String ssoId, int projectId) {
        List<Privileg> pList = PrivilegFacade.getList(projectId);
        for(int i=0;i<pList.size();i++) {
            if(pList.get(i).getAdmin().booleanValue()==true) {
                int groupId = userHasPrivileg(ssoId,pList.get(i).getId().intValue());
                if(groupId!=-1) {
                    return groupId;
                }
            }
        }
        return -1;
    }

    public static List<Privileg> getUserPrivileges(String ssoId) {

        //Getting sso object
        SSO sso = get(ssoId);
        //Getting User id
        int userId = sso.getUserId();
        //Getting groups of user
        List<UserGroup> ugList = UserGroupFacade.getList1(userId);
        List<Privileg> pList = new ArrayList<Privileg>();
        for(int i=0;i<ugList.size();i++) {
            UserGroup ug = ugList.get(i);
            List<GroupPrivileg> gpList = GroupPrivilegFacade.getList(ug.getGroupId());
            for(int j=0;j<gpList.size();j++) {
                GroupPrivileg gp = gpList.get(j);
                pList.add(PrivilegFacade.get(gp.getPrivilegId()));
            }
        }
        return pList;

    }

    public static int userHasPrivileg(String ssoId, int privilegId) {
        //Getting alle Groups that have privileg
        List<GroupPrivileg> upList = GroupPrivilegFacade.getList1(privilegId);
        if(upList.size()==0) {
            return -1;
        }

        ///Getting single sign on object to have user information
        SSO sso = get(ssoId);

        //Getting all groups of the user
        List<UserGroup> ugList = UserGroupFacade.getList1(sso.getUserId());

        //Checking whether group of user is in list of groups that have privileg
        for(int i=0;i<ugList.size();i++) {
            for(int j=0;j<upList.size();j++) {
                if(upList.get(j).getGroupId()==ugList.get(i).getGroupId()) {
                    return upList.get(j).getGroupId();
                }
            }
        }
        return -1;
    }

    public static int userHasPrivileg(String ssoId, String privilegName, int projectId) {
        if(!sessionIsActiv(ssoId)) {
            return -1;
        }

        //Getting the privileg which has $privilegName
        Privileg privileg = PrivilegFacade.get(projectId, privilegName);
        if(privileg==null) {
            return -1;
        }

        //Checking whether user has privileg
        int groupId1 = userHasPrivileg(ssoId, projectId);
        if(groupId1!=-1) {
            return groupId1;
        }

        //Checking whether user has admin privileg
        int groupId2 = userIsAdmin(ssoId, projectId);
        if(groupId2!=-1) {
            return groupId2;
        }
        
        return -1;
    }

    public static List<Group> getGroup(String ssoId) {
        SSO sso = get(ssoId);
        int userId = sso.getUserId();
        List<UserGroup> ugList = UserGroupFacade.getList1(userId);
        List<Group> gList = new ArrayList<Group>();
        for(int i=0;i<ugList.size();i++) {
            gList.add(GroupFacade.get(ugList.get(i).getGroupId()));
        }
        return gList;
    }

    public static int userHasAdminProjectPrivileg(String ssoId) {
        List<Group> gList = getGroup(ssoId);
        for(int i=0;i<gList.size();i++) {
            Group group = gList.get(i);
            if(group.getAdminProject().booleanValue() || group.getAdminAll()) {
                return group.getId().intValue();
            }
        }
        return -1;
    }

    public static int userHasAdminGroupPrivileg(String ssoId) {
        List<Group> gList = getGroup(ssoId);
        for(int i=0;i<gList.size();i++) {
            Group group = gList.get(i);
            if(group.getAdminGroup().booleanValue() || group.getAdminAll()) {
                return group.getId().intValue();
            }
        }
        return -1;
    }

    public static int userHasAdminCustomerPrivileg(String ssoId) {
        List<Group> gList = getGroup(ssoId);
        for(int i=0;i<gList.size();i++) {
            Group group = gList.get(i);
            if(group.getAdminCustomer().booleanValue() || group.getAdminAll()) {
                return group.getId().intValue();
            }
        }

        return -1;
    }

    public static int userHasAdminUserPrivileg(String ssoId) {
        List<Group> gList = getGroup(ssoId);
        for(int i=0;i<gList.size();i++) {
            Group group = gList.get(i);
            if(group.getAdminUser().booleanValue() || group.getAdminAll()) {
                return group.getId().intValue();
            }
        }
        return -1;
    }

    public static int userHasAdminAll(String ssoId) {
        List<Group> gList = getGroup(ssoId);
        for(int i=0;i<gList.size();i++) {
            Group group = gList.get(i);
            if(group.getAdminAll()) {
                return group.getId().intValue();
            }
        }
        return -1;
    }

}